<template>
  <div class="">
    <headMain :mensid="'2'" />
    <div class="mentactive">
      <div class="location">
        <img src="@/assets/images/location.png" alt="" />
      </div>
      <div>当前位置：</div>
      <div class="mentname1" @click="goback">病例学习></div>
      <div class="mentname2">{{ caseTypeName }}</div>
    </div>

    <!-- 主题内容展示区域 -->
    <div class="casebox">
      <div class="casesearch"
        >病例名称：
        <a-input-search
          :style="{ width: '249px' }"
          placeholder="请输入病例名称"
          v-model="caseName"
        />
        <div class="casesearchbt" @click="fetchData">搜索</div>
        <div class="casesearchbt1" @click="rest">重置</div>
      </div>
      <div class="casemain">
        <div v-for="item in rendata" class="casemainitem" @click="goitem(item)">
          <div class="casemainitemimg">
            <img :src="base + item.url" alt="" />
          </div>
          <div class="caseitemdes">
            <div class="caseitemtitle"> {{ item.caseName }}</div>
            <div class="caseitemdestext">{{ item.situation }}</div>
            <div class="caseitemfa" @click.stop="favoritefn(item)">
              <img
                src="@/assets/images/faac.png"
                alt=""
                v-if="item.favoriteStatus != 0"
              />
              <img
                src="@/assets/images/fa.png"
                alt=""
                v-if="item.favoriteStatus == 0"
              />
              {{ item.favoriteStatus == 0 ? '取消收藏' : '收藏' }}</div
            >
          </div>
        </div>
      </div>
      <div class="pagbox"
        ><a-pagination
          :total="total"
          :current="current"
          :page-size="pageSize"
          show-total
          show-jumper
          show-page-size
          @change="nextpage"
          @page-size-change="pageSizefn"
          :page-size-options="[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12]"
        />
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import headMain from '@/views/headmain/index.vue';
  import { caseList, favoriteAdd, favoriteDel } from '@/api/case';
  import { ref } from 'vue';
  import { useRouter } from 'vue-router';
  import { useRoute } from 'vue-router';
  import useUser from '@/hooks/user';
  import { storeToRefs } from 'pinia';
  import { Message, Modal, TreeNodeData } from '@arco-design/web-vue';
  import { useUserStore } from '@/store';
  const userStore = useUserStore();
  const userdata = storeToRefs(userStore);
  const studentdata = ref(userdata);
  const router = useRouter();
  const route = useRoute();
  const caseTypeName = ref(route.query.caseTypeName);
  const base = ref((window as any).config.VUE_APP_IMG_API);
  const caseTypeId = ref(route.query.caseTypeId);
  const total = ref();
  const pageSize = ref(6);
  const current = ref(1);
  const rendata = ref();
  const caseName = ref('');
  const fetchData = () => {
    caseList({
      caseTypeId: caseTypeId.value,
      pageSize: pageSize.value,
      pageNum: current.value,
      caseName: caseName.value,
      studentId: studentdata.value.studentId,
    }).then((res: any) => {
      if (res.code == 200) {
        rendata.value = res.data.list;
        total.value = res.data.total;
        current.value = res.data.pageNum;
        pageSize.value = res.data.pageSize;
      }
    });
  };
  fetchData();

  const nextpage = (i) => {
    current.value = i;
    fetchData();
  };
  const pageSizefn = (i) => {
    pageSize.value = i;
    fetchData();
  };
  const rest = () => {
    current.value = 1;
    pageSize.value = 6;
    caseName.value = '';
    fetchData();
  };

  // 添加到收藏夹
  const favoritefn = (item) => {
    if (item.favoriteFolderId) {
      favoriteDel({
        favoriteFolderIds: item.favoriteFolderId,
      }).then((res: any) => {
        if (res.code == 200) {
          Message.success('取消收藏');
          fetchData();
        }
      });
    } else {
      favoriteAdd({
        studentId: studentdata.value.studentId,
        caseAtlasId: item.caseId,
        status: 0,
      }).then((res: any) => {
        if (res.code == 200) {
          Message.success('收藏成功');
          fetchData();
        }
      });
    }
  };
  //   返回病例分类
  const goback = () => {
    router.push({
      name: 'casetype',
    });
  };

  const goitem = (item: any) => {
    router.push({
      name: 'case',
      query: {
        caseTypeId: caseTypeId.value,
        caseName: item.caseName,
        caseTypeName: caseTypeName.value,
        caseId: item.caseId,
        favoriteStatus: item.favoriteStatus,
        favoriteFolderId: item.favoriteFolderId,
      },
    });
  };
</script>

<style scoped>
  .mentactive {
    color: #666666;
    display: flex;
    align-items: center;
    font-size: 16px;
    margin-top: 40px;
    margin-left: 73px;
  }
  .location img {
    width: 16px;
    height: 16px;
  }
  .mentname1 {
    font-size: 22px;
    cursor: pointer;
  }
  .mentname2 {
    font-size: 22px;
    color: black;
  }
  .casebox {
    width: 1774px;
    height: 734px;
    background: #e8ebee;
    border-radius: 10px 10px 10px 10px;
    border: 1px solid #a3b0c1;
    margin: 20px auto;
  }
  .casemain {
    display: flex;
    flex-wrap: wrap; /* 允许换行 */
    height: 650px;
    width: 100%;
    overflow-y: auto;
    gap: 10px; /* 可选，设置间距 */
  }

  .casemain {
    display: flex;
    flex-wrap: wrap; /* 允许换行 */
    justify-content: flex-start; /* 从左到右排列 */
    gap: 30px; /* 控制间距 */
    height: 600px;
    width: 100%;
    overflow-y: auto;
    padding: 36px;
  }

  .casemainitem {
    width: 546px;
    height: 246px;
    background: #ffffff;
    border-radius: 10px;
    display: flex;
    padding: 10px;
    cursor: pointer;
  }
  .casesearch {
    font-size: 14px;
    color: #666666;
    display: flex;
    align-items: center;
    margin-top: 30px;
    margin-left: 36px;
  }

  .casesearchbt {
    font-size: 12px;
    color: #ffffff;
    width: 60px;
    height: 34px;
    line-height: 34px;
    text-align: center;
    background: linear-gradient(180deg, #748191 0%, #242834 100%);
    border-radius: 4px 4px 4px 4px;
    margin: 0px 5px;
    cursor: pointer;
  }
  .casesearchbt1 {
    text-align: center;
    line-height: 34px;
    font-size: 12px;
    color: #ffffff;
    width: 60px;
    height: 34px;
    background: linear-gradient(180deg, #b3c1d2 0%, #748191 100%);
    border-radius: 4px 4px 4px 4px;
    cursor: pointer;
  }
  .pagbox {
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .casemainitemimg {
    width: 217px;
    height: 204px;
    border-radius: 5px;
    overflow: hidden;
    margin: 10px;
  }
  .casemainitemimg img {
    width: 217px;
    height: 204px;
    border-radius: 5px;
    overflow: hidden;
  }
  .caseitemdes {
    display: flex;
    flex-direction: column;
    padding: 15px 0;
  }
  .caseitemtitle {
    font-size: 18px;
    color: #333333;

    width: 280px;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
  }
  .caseitemdestext {
    width: 280px;
    font-size: 16px;
    color: #666666;
    height: 134px;
    margin: 10px 0;
  }
  .caseitemfa {
    font-size: 14px;
    color: #999999;
    display: flex;
    align-items: center;
    cursor: pointer;
  }
  .caseitemfa img {
    width: 20px;
    height: 20px;
  }
</style>
